home *** CD-ROM | disk | FTP | other *** search
- Subject: Re: random number in assembly
- References: <Pine.NEB.3.91.960327200012.12939A-100000@dirty.cute.fi>
- X-Newsreader: TIN [version 1.2 PL2]
- Path: imada.ou.dk!breese
- From: breese@imada.ou.dk (Bjorn Reese)
- Message-ID: <1996Mar28.133904.5493@imada.ou.dk>
- Sender: news@imada.ou.dk
- Nntp-Posting-Host: wagner.imada.ou.dk
- Organization: Dept. of Math. & Computer Science, Odense University, Denmark
- Date: Thu, 28 Mar 1996 13:39:04 GMT
- Newsgroups: comp.sys.amiga.programmer
-
- Kristian Slavov (kslavov@dirty.cute.fi) wrote:
- > Hi!
- > Could someone tell me how to get a VERY WELL randomized number?
-
- Here's one: 2 :)
-
- > I've tried all kinds of methods but they are not enough random :(
- > To make the problem a little bit harder I need a routine that can be used
- > in loops so that the numbers wouldn't be for ex. 4,8,12,16,20 or
- > 2,3,4,5,6,7. Hope you understood that :)
-
- Here's my favorite (unfortunately in C, but it should be easy to
- translate to asm - it's originates from Knuth, and was done in some
- kind of asm)
-
- InitRandom() initializes the rndValues array with random numbers.
- These numbers are picked and altered by Random(). Random() has
- a long sequence and is quick.
-
-
-
- /* RNDSIZE must be an integral number of 2 (and at least 8) */
- #define RNDSIZE 64
- #define RNDMASK RNDSIZE-1
-
- static RND rnd1;
- static RND rnd2;
- static RND rndValues[RNDSIZE];
-
- /* --- Random ----------------------------------------------------- */
-
- inline RND Random(void)
- {
- rnd1 = (rnd1 - 1) & RNDMASK;
- rnd2 = (rnd2 - 1) & RNDMASK;
- return (rndValues[rnd2] += rndValues[rnd1]);
- }
-
- /* --- InitRandom ------------------------------------------------- */
-
- void InitRandom(int s)
- {
- int i;
-
- for (i = 0; i < RNDSIZE; i++) {
- rndValues[i] = (RND)s;
- /* --- randqd1() from Numerical Recipes --- */
- s = 0x0019660dL * s + 0x3c6ef35fL;
- }
- rnd1 = 0;
- rnd2 = RNDSIZE/2 - 3;
-
- /* --- Remove initial errors of bad seeding (warn-up) --- */
- for (i = 0; i < RNDSIZE; i++) Random();
- }
-
- --
- Bjorn Reese Email: breese@imada.ou.dk
- Odense University, Denmark URL: http://www.imada.ou.dk/~breese
-
- "It's getting late in the game to show any pride or shame" - Marillion
-